﻿package com.flashwiitanks
{
	import flash.display.*;
	import flash.events.*;
	import flash.ui.*;
	
	public class PlayerTank extends MovieClip
	{		
		//number of bullets
		var clip:Number = 5;
		
		//speed at which the tank moves
		var movementSpeed:Number = 3;
		var main:MovieClip;
		var gun:MovieClip;

		var keyPressed:uint;
		var rightKeyIsDown:Boolean;
		var leftKeyIsDown:Boolean;
		var upKeyIsDown:Boolean
		var downKeyIsDown:Boolean;
		
		public function PlayerTank() {
			//when player is added to the stage
			this.addEventListener(Event.ADDED, initialize)
		}
		
		public function initialize(event:Event):void {
			//get the main stage
			main = MovieClip(this.parent);
			gun = gun_mc;
			
			//add listeners to move tank
			stage.addEventListener(KeyboardEvent.KEY_DOWN, pressKey);
			this.addEventListener(Event.ENTER_FRAME, moveTank);
			stage.addEventListener(KeyboardEvent.KEY_UP, releaseKey);
			
			//add listener for player to shoot a bullet
			stage.addEventListener(MouseEvent.CLICK, this.shootBullet);
		}

		function pressKey(event:KeyboardEvent):void {
			keyPressed = event.keyCode;
			
			if(keyPressed == Keyboard.RIGHT || keyPressed == 68) {
				rightKeyIsDown = true;
			}
			if(keyPressed == Keyboard.LEFT || keyPressed == 65) {
				leftKeyIsDown = true;
			}	
			if(keyPressed == Keyboard.UP || keyPressed == 87) {
				upKeyIsDown = true;
			}
			if(keyPressed == Keyboard.DOWN || keyPressed == 83) {
				downKeyIsDown = true;
			}
		}
		
		function releaseKey(event:KeyboardEvent):void {
			var thisKey:uint = event.keyCode;
			
			
			if(thisKey == Keyboard.RIGHT || thisKey == 68) {
				rightKeyIsDown = false;
			}
			if(thisKey == Keyboard.LEFT || thisKey == 65) {
				leftKeyIsDown = false;
			}	
			if(thisKey == Keyboard.UP || thisKey == 87) {
				upKeyIsDown = false;
			}
			if(thisKey == Keyboard.DOWN || thisKey == 83) {
				downKeyIsDown = false;
			}
		}
		
		function moveTank(event:Event):void {			
			if(rightKeyIsDown) {
				//if hitting right boundary or a wall, dont move
				if((this.x >= (stage.stageWidth - (this.width / 2))) || hittingWallRight()) {
					//dont move
				} else {
					//move tank
					this.x += movementSpeed;
				}
			}
			if(leftKeyIsDown) {
				//if hitting left boundary or a wall, dont move
				if(((this.x - this.width / 2) <= 0) || hittingWallLeft()) {
					//dont move
				} else {
					//move tank
					this.x -= movementSpeed;
				}
			}
			if(upKeyIsDown) {
				//if hitting top boundary or a wall, dont move
				if(((this.y - (this.height / 2)) <= 0) || hittingWallUp()) {
					//dont move
				} else {
					//move tank
					this.y -= movementSpeed;
				}
			}
			if(downKeyIsDown) {
				//if hitting bottom boundary or a wall, dont move
				if((this.y >= (stage.stageHeight - (this.height / 2))) || hittingWallDown()) {
					//dont move
				} else {
					//move tank
					this.y += movementSpeed;
				}
			}
		}
		
		/* moveTank function that has touchy/buggy wall collisions, rotattion and movment at 8 angles
		function moveTank(event:Event):void {
			//if up right
			if(rightKeyIsDown && upKeyIsDown && !leftKeyIsDown && !downKeyIsDown) {
				//if hitting top or right stage boundary or a wall
				if((this.x >= (stage.stageWidth - (this.width / 2))) || ((this.y - (this.height / 2)) <= 0) || hittingWallUp() || hittingWallRight())  {
					//dont move
				} else {
					//if tank is rotated to move up right
					//then move tank
					//else rotate
					if(this.rotation == -45 || this.rotation == 135) {
						//move tank
						this.x += movementSpeed;
						this.y -= movementSpeed;
						return;
					} else {
						//if tank rotation is within one rotation movement, then set the rotation
						if((this.rotation < (135 + movementSpeed * 2)) && (this.rotation > (135 - movementSpeed * 2))) {
							this.rotation = 135;
							return;
						} else if((this.rotation > (-45 - movementSpeed * 2)) && (this.rotation < (-45 + movementSpeed * 2))) {
							this.rotation = -45;
							return;
						}
						
						//if tank is LEFT UP or between LEFT UP,RIGHT UP or LEFT DOWN,RIGHT DOWN, then add to the rotation
						//else if tank is RIGHT DOWN or between UP RIGHT,DOWN RIGHT or DOWNLEFT,UP LEFT, then subtract rotation
						if((this.rotation == -135) || (this.rotation < -45 && this.rotation > -135) || (this.rotation < 135 && this.rotation > 45)) {
							this.rotation += movementSpeed * 2;
							return;
						} else if((this.rotation == 45) || (this.rotation > -45 && this.rotation < 45) || (this.rotation > 135) || (this.rotation < -135)) {
							this.rotation -= movementSpeed * 2;
							return;
						}						
					}
				}
			}
			
			//if down right
			if(rightKeyIsDown && !upKeyIsDown && !leftKeyIsDown && downKeyIsDown) {
				//if hitting bottom or right stage boundary or a wall
				if((this.x >= (stage.stageWidth - (this.width / 2))) || (this.y >= (stage.stageHeight - (this.height / 2))) || hittingWallDown() || hittingWallRight())  {
					//dont move
				} else {
					//if tank is rotated to move down right
					//then move tank
					//else rotate
					if(this.rotation == 45 || this.rotation == -135) {
						//move tank
						this.x += movementSpeed;
						this.y += movementSpeed;
						return;
					} else {
						//if tank rotation is within one rotation movement, then set the rotation
						if((this.rotation < (45 + movementSpeed * 2)) && (this.rotation > (45 - movementSpeed * 2))) {
							this.rotation = 45;
							return;
						} else if((this.rotation > (-135 - movementSpeed * 2)) && (this.rotation < (-135 + movementSpeed * 2))) {
							this.rotation = -135;
							return;
						}
						
						//if tank is RIGHT UP or between RIGHT UP,RIGHT DOWN or between LEFT and LEFT UP, then add to the rotation
						//else if tank is RIGHT DOWN or between LEFT DOWN,RIGHT DOWN or LEFT UP,RIGHT UP, then subtract rotation
						if((this.rotation == -45) || (this.rotation > -45 && this.rotation < 45) || (this.rotation > 135) || (this.rotation < -135)) {
							this.rotation += movementSpeed * 2;
							return;
						} else if((this.rotation == 135) || (this.rotation > 45 && this.rotation < 135) || (this.rotation < -45 && this.rotation > -135)) {
							this.rotation -= movementSpeed * 2;
							return;
						}						
					}
				}
			}
			
			//if up left
			if(!rightKeyIsDown && upKeyIsDown && leftKeyIsDown && !downKeyIsDown) {
				//if hitting up or left stage boundary or a wall
				if(((this.x - (this.width / 2)) <= 0) || ((this.y - (this.height / 2)) <= 0) || hittingWallUp() || hittingWallLeft())  {
					//dont move
				} else {
					//if tank is rotated to move up left
					//then move tank
					//else rotate
					if(this.rotation == 45 || this.rotation == -135) {
						//move tank
						this.x -= movementSpeed;
						this.y -= movementSpeed;
						return;
					} else {
						//if tank rotation is within one rotation movement, then set the rotation
						if((this.rotation < (45 + movementSpeed * 2)) && (this.rotation > (45 - movementSpeed * 2))) {
							this.rotation = 45;
							return;
						} else if((this.rotation > (-135 - movementSpeed * 2)) && (this.rotation < (-135 + movementSpeed * 2))) {
							this.rotation = -135;
							return;
						}
						
						//if tank is RIGHT UP or between RIGHT UP,RIGHT DOWN or between LEFT and LEFT UP, then add to the rotation
						//else if tank is RIGHT DOWN or between LEFT DOWN,RIGHT DOWN or LEFT UP,RIGHT UP, then subtract rotation
						if((this.rotation == 135) || (this.rotation > -45 && this.rotation < 45) || (this.rotation > 135) || (this.rotation < -135)) {
							this.rotation += movementSpeed * 2;
							return;
						} else if((this.rotation == -45) || (this.rotation > 45 && this.rotation < 135) || (this.rotation < -45 && this.rotation > -135)) {
							this.rotation -= movementSpeed * 2;
							return;
						}						
					}
				}
			}
			
			//if down left
			if(!rightKeyIsDown && !upKeyIsDown && leftKeyIsDown && downKeyIsDown) {
				//if hitting bottom or left stage boundary or a wall
				if(((this.x - (this.width / 2)) <= 0) || (this.y >= (stage.stageHeight - (this.height / 2))) || hittingWallDown() || hittingWallLeft())  {
					//dont move
				} else {
					//if tank is rotated to move up right
					//then move tank
					//else rotate
					if(this.rotation == -45 || this.rotation == 135) {
						//move tank
						this.x -= movementSpeed;
						this.y += movementSpeed;
						return;
					} else {
						//if tank rotation is within one rotation movement, then set the rotation
						if((this.rotation < (135 + movementSpeed * 2)) && (this.rotation > (135 - movementSpeed * 2))) {
							this.rotation = 135;
							return;
						} else if((this.rotation > (-45 - movementSpeed * 2)) && (this.rotation < (-45 + movementSpeed * 2))) {
							this.rotation = -45;
							return;
						}
						
						//if tank is RIGHT DOWN or between LEFT UP,RIGHT UP or LEFT DOWN,RIGHT DOWN, then add to the rotation
						//else if tank is UP LEFT or between UP RIGHT,DOWN RIGHT or DOWNLEFT,UP LEFT, then subtract rotation
						if((this.rotation == 45) || (this.rotation < -45 && this.rotation > -135) || (this.rotation < 135 && this.rotation > 45)) {
							this.rotation += movementSpeed * 2;
							return;
						} else if((this.rotation == -135) || (this.rotation > -45 && this.rotation < 45) || (this.rotation > 135) || (this.rotation < -135)) {
							this.rotation -= movementSpeed * 2;
							return;
						}						
					}
				}
			}
			
			if(rightKeyIsDown) {
				//if hitting right boundary or a wall, dont move
				if((this.x >= (stage.stageWidth - (this.width / 2))) || hittingWallRight()) {
					//dont move
				} else {
					//if tank is rotated to move right
					//then move tank
					//else rotate
					if(this.rotation == 0 || this.rotation == 180) {
						//move tank
						this.x += movementSpeed;
						return;
					} else {
						//if tank rotation is within one rotation movement, then set the rotation
						if((this.rotation < (0 + movementSpeed * 2)) && (this.rotation > (0 - movementSpeed * 2))) {
							this.rotation = 0;
							return;
						} else if((this.rotation > (180 - movementSpeed * 2)) || (this.rotation < (-180 + movementSpeed * 2))) {
							this.rotation = 180;
							return;
						}
						
						//if tank is UP or between UP,RIGHT or DOWN,LEFT quadrants, then add to the rotation
						//else if tank is DOWN or between UP,LEFT or DOWN,RIGHT, then subtract rotation
						if((this.rotation == -90) || (this.rotation < 0 && this.rotation > -90) || (this.rotation < 180 && this.rotation > 90)) {
							this.rotation += movementSpeed * 2;
							return;
						} else if((this.rotation == 90) || (this.rotation > -180 && this.rotation < -90) || (this.rotation > 0 && this.rotation < 90)) {
							this.rotation -= movementSpeed * 2;
							return;
						}						
					}
				}
			}
			if(leftKeyIsDown) {
				//if hitting left boundary or a wall, dont move
				if(((this.x - this.width / 2) <= 0) || hittingWallLeft()) {
					//dont move
				} else {
					//if tank is rotated to move left
					//then move tank
					//else rotate
					if(this.rotation == 0 || this.rotation == 180) {
						//move tank
						this.x -= movementSpeed;
						return;
					} else {
						//if tank rotation is within one rotation movement, then set the rotation
						if((this.rotation < (0 + movementSpeed * 2)) && (this.rotation > (0 - movementSpeed * 2))) {
							this.rotation = 0;
							return;
						} else if((this.rotation > (180 - movementSpeed * 2)) || (this.rotation < (-180 + movementSpeed * 2))) {
							this.rotation = 180;
							return;
						}
						
						//if tank is UP or between UP,RIGHT or DOWN,LEFT quadrants, then add to the rotation
						//else if tank is DOWN or between UP,LEFT or DOWN,RIGHT, then subtract rotation
						if((this.rotation == 90) || (this.rotation < 0 && this.rotation > -90) || (this.rotation < 180 && this.rotation > 90)) {
							this.rotation += movementSpeed * 2;
							return;
						} else if((this.rotation == -90) || (this.rotation > -180 && this.rotation < -90) || (this.rotation > 0 && this.rotation < 90)) {
							this.rotation -= movementSpeed * 2;
							return;
						}						
					}
				}
			}
			if(upKeyIsDown) {
				//if hitting top boundary or a wall, dont move
				if(((this.y - (this.height / 2)) <= 0) || hittingWallUp()) {
					//dont move
				} else {
					//if tank is rotated to move up
					//then move tank
					//else rotate
					if(this.rotation == 90 || this.rotation == -90) {
						//move tank
						this.y -= movementSpeed;
						return;
					} else {
						//if tank rotation is within one rotation movement, then set the rotation
						if((this.rotation < (90 + movementSpeed * 2)) && (this.rotation > (90 - movementSpeed * 2))) {
							this.rotation = 90;
							return;
						} else if(((this.rotation < (-90 + movementSpeed * 2)) && (this.rotation > (-90 - movementSpeed * 2)))) {
							this.rotation = -90;
							return;
						}
						
						//if tank is LEFT or between UP,LEFT or DOWN,RIGHT quadrants, then add to the rotation
						//else if tank is RIGHT or between UP,RIGHT or DOWN,LEFT, then subtract rotation
						if((this.rotation == 180 || this.rotation == -180) || (this.rotation > -180 && this.rotation < -90) || (this.rotation > 0 && this.rotation < 90)) {
							this.rotation += movementSpeed * 2;
							return;
						} else if((this.rotation == 0) || (this.rotation < 0 && this.rotation > -90) || (this.rotation < 180 && this.rotation > 90)) {
							this.rotation -= movementSpeed * 2;
							return;
						}						
					}
				}
			}
			if(downKeyIsDown) {
				//if hitting bottom boundary or a wall, dont move
				if((this.y >= (stage.stageHeight - (this.height / 2))) || hittingWallDown()) {
					//dont move
				} else {
					//if tank is rotated to move down
					//then move tank
					//else rotate
					if(this.rotation == 90 || this.rotation == -90) {
						//move tank
						this.y += movementSpeed;
						return;
					} else {
						//if tank rotation is within one rotation movement, then set the rotation
						if((this.rotation < (90 + movementSpeed * 2)) && (this.rotation > (90 - movementSpeed * 2))) {
							this.rotation = 90;
							return;
						} else if(((this.rotation < (-90 + movementSpeed * 2)) && (this.rotation > (-90 - movementSpeed * 2)))) {
							this.rotation = -90;
							return;
						}
						
						//if tank is LEFT or between UP,LEFT or DOWN,RIGHT quadrants, then add to the rotation
						//else if tank is RIGHT or between UP,RIGHT or DOWN,LEFT, then subtract rotation
						if((this.rotation == 0) || (this.rotation > -180 && this.rotation < -90) || (this.rotation > 0 && this.rotation < 90)) {
							this.rotation += movementSpeed * 2;
							return;
						} else if((this.rotation == 180 || this.rotation == -180) || (this.rotation < 0 && this.rotation > -90) || (this.rotation < 180 && this.rotation > 90)) {
							this.rotation -= movementSpeed * 2;
							return;
						}						
					}
				}
			}
		}
		*/
		
		public function shootBullet(event:MouseEvent):void {
			if(clip > 0) {
				var bullet:MovieClip = new Bullet(this);
				clip--;
			}
		}		
		
		//when a bullet is destroyed, the clip is increased to allow another bullet to fire
		public function increaseClip():void {
			clip++;
		}
		
		//if this tank is getting hit with a bullet, it should be killed
		public function kill():void {
			//hide tank
			this.visible = false;
			
			main.endGame("Try Again?");
		}
		
		public function suspend():void {			
		
			//add listeners to move tank
			stage.removeEventListener(KeyboardEvent.KEY_DOWN, pressKey);
			stage.removeEventListener(KeyboardEvent.KEY_UP, releaseKey);
			this.removeEventListener(Event.ENTER_FRAME, moveTank);
			
			//add listener for player to shoot a bullet
			stage.removeEventListener(MouseEvent.CLICK, this.shootBullet);
		}
		
		public function resume():void {
			//add listeners to move tank
			stage.addEventListener(KeyboardEvent.KEY_DOWN, pressKey);
			stage.addEventListener(KeyboardEvent.KEY_UP, releaseKey);
			this.addEventListener(Event.ENTER_FRAME, moveTank);
			
			//add listener for player to shoot a bullet
			stage.addEventListener(MouseEvent.CLICK, this.shootBullet);
		}
		
		//loop through walls in the stage and return true if colliding with going up
		public function hittingWallUp():Boolean {
			for(var i:int = 0; i < main.walls.numChildren; i++) {
				var wall:MovieClip = main.walls.getChildAt(i);
				
				//if hitting a wall
				if(this.hitTestObject(wall)) {
					var wallLeft:Number = wall.x - (wall.width / 2) - 5;
					var wallRight:Number = wall.x + (wall.width / 2) + 5;
					var wallTop:Number = wall.y - (wall.height / 2)- 5;
					var wallBottom:Number = wall.y + (wall.height / 2) + 5;
					
					var tankLeft:Number = this.x - (this.width / 2);
					var tankRight:Number = this.x + (this.width /2);
					var tankTop:Number = this.y - (this.height / 2);
					var tankBottom:Number = this.y + (this.height / 2);
					
					//if tank is aligned to hit wall
					if((tankLeft > wallLeft && tankLeft < wallRight) || (tankRight < wallRight && tankRight > wallLeft)) {
						//if bottom of tank is below bottom of wall, then tank must be colliding with the wall going up
						if(tankBottom > wallBottom) {
							return true;
						}
					}
				}
			}
			return false;
		}
		
		//loop through walls in the stage and return true if colliding with one
		public function hittingWallDown():Boolean {
			for(var i:int = 0; i < main.walls.numChildren; i++) {
				var wall:MovieClip = main.walls.getChildAt(i);
				
				//if hitting a wall
				if(this.hitTestObject(wall)) {
					var wallLeft:Number = wall.x - (wall.width / 2) - 5;
					var wallRight:Number = wall.x + (wall.width / 2) + 5;
					var wallTop:Number = wall.y - (wall.height / 2)- 5;
					var wallBottom:Number = wall.y + (wall.height / 2) + 5;
					
					var tankLeft:Number = this.x - (this.width / 2);
					var tankRight:Number = this.x + (this.width /2);
					var tankTop:Number = this.y - (this.height / 2);
					var tankBottom:Number = this.y + (this.height / 2);
					
					//if tank is aligned to hit wall
					if((tankLeft > wallLeft && tankLeft < wallRight) || (tankRight < wallRight && tankRight > wallLeft)) {
						//if top of tank is above top of wall, then tank must be colliding with the wall going down
						if(tankTop < wallTop) {
							return true;
						}
					}
				}
			}
			return false;
		}
		
		//loop through walls in the stage and return true if colliding with one
		public function hittingWallRight():Boolean {
			for(var i:int = 0; i < main.walls.numChildren; i++) {
				var wall:MovieClip = main.walls.getChildAt(i);
				
				//if hitting a wall
				if(this.hitTestObject(wall)) {
					var wallLeft:Number = wall.x - (wall.width / 2) - 5;
					var wallRight:Number = wall.x + (wall.width / 2) + 5;
					var wallTop:Number = wall.y - (wall.height / 2)- 5;
					var wallBottom:Number = wall.y + (wall.height / 2) + 5;
					
					var tankLeft:Number = this.x - (this.width / 2);
					var tankRight:Number = this.x + (this.width /2);
					var tankTop:Number = this.y - (this.height / 2);
					var tankBottom:Number = this.y + (this.height / 2);
					
					//if tank is aligned to hit wall
					if((tankTop < wallTop && tankBottom > wallTop) || (tankBottom > wallBottom && tankTop < wallBottom) || (tankTop > wallTop && tankTop < wallBottom)) {
						//if left of tank is left of the left of the wall, then tank must be colliding with the wall going right
						if(tankLeft < wallLeft) {
							return true;
						}
					}
				}
			}
			return false;
		}
		
		//loop through walls in the stage and return true if colliding with one
		public function hittingWallLeft():Boolean {
			for(var i:int = 0; i < main.walls.numChildren; i++) {
				var wall:MovieClip = main.walls.getChildAt(i);
				
				//if hitting a wall
				if(this.hitTestObject(wall)) {
					var wallLeft:Number = wall.x - (wall.width / 2) - 5;
					var wallRight:Number = wall.x + (wall.width / 2) + 5;
					var wallTop:Number = wall.y - (wall.height / 2)- 5;
					var wallBottom:Number = wall.y + (wall.height / 2) + 5;
					
					var tankLeft:Number = this.x - (this.width / 2);
					var tankRight:Number = this.x + (this.width /2);
					var tankTop:Number = this.y - (this.height / 2);
					var tankBottom:Number = this.y + (this.height / 2);
					
					//if tank is aligned to hit wall
					if((tankTop < wallTop && tankBottom > wallTop) || (tankBottom > wallBottom && tankTop < wallBottom) || (tankTop > wallTop && tankTop < wallBottom)) {
						//if right of tank is right of the right of the wall, then tank must be colliding with the wall going left
						if(tankRight > wallRight) {
							return true;
						}
					}
				}
			}
			return false;
		}
	}
}